type FlattenDepth<
  T extends any[],
  D extends number = 1,
  U extends any[] = []
> = U["length"] extends D
  ? T
  : T extends [infer F, ...infer R]
  ? F extends any[]
    ? [...FlattenDepth<F, D, [...U, 1]>, ...FlattenDepth<R, D, U>]
    : [F, ...FlattenDepth<R, D, U>]
  : T;

// type Fd = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>
type Fd = FlattenDepth<[1, 2, [3, 4], [[[5]]]], 2>;
